package com.example.hrsystem.ui.approval.businesstravel;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hrsystem.R;
import com.example.hrsystem.workflow.ApprovalViewModel;
import com.example.hrsystem.workflow.model.BusinessTravelRequest;
// 修正导入路径 - 使用正确的包路径
// 注：根据项目结构，BusinessTripRequest可能在workflow.model包中

import java.util.ArrayList;
import java.util.List;

/**
 * 出差申请列表Activity
 * 显示用户所有出差申请记录
 */
public class BusinessTravelRequestListActivity extends AppCompatActivity {
    private ApprovalViewModel viewModel;
    private RecyclerView recyclerView;
    private BusinessTravelRequestAdapter adapter;
    private ProgressBar progressBar;
    private TextView emptyStateTextView;
    private TextView errorTextView;
    private Button retryButton;
    private Button newRequestButton;
    
    private int currentUserId = 1000; // 默认用户ID，实际应用中应从登录信息获取
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business_travel_request_list);

        // 初始化ViewModel
        viewModel = new ViewModelProvider(this).get(ApprovalViewModel.class);

        // 初始化UI组件
        initUI();

        // 设置RecyclerView
        setupRecyclerView();

        // 设置按钮点击事件
        setupButtonListeners();

        // 加载出差申请数据
        loadBusinessTravelRequests();
    }

    /**
     * 初始化UI组件
     */
    private void initUI() {
        recyclerView = findViewById(R.id.recycler_view);
        progressBar = findViewById(R.id.progress_bar);
        // 暂时注释掉对不存在资源的引用
        // emptyStateTextView = findViewById(R.id.empty_state_text_view);
        errorTextView = findViewById(R.id.error_message_text_view);
        retryButton = findViewById(R.id.retry_button);
        newRequestButton = findViewById(R.id.new_request_button);
    }

    /**
     * 设置RecyclerView
     */
    private void setupRecyclerView() {
        adapter = new BusinessTravelRequestAdapter(new ArrayList<>(), new BusinessTravelRequestAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BusinessTravelRequest request) {
                // 点击出差申请项，跳转到详情页
                // 暂时注释掉对不存在类的引用
                // Intent intent = new Intent(BusinessTravelRequestListActivity.this, BusinessTravelRequestDetailActivity.class);
                // intent.putExtra("business_travel_request_id", request.getId());
                // startActivity(intent);
                Toast.makeText(BusinessTravelRequestListActivity.this, "出差申请详情页面功能暂不可用", Toast.LENGTH_SHORT).show();
            }
        });
        
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    /**
     * 设置按钮点击事件
     */
    private void setupButtonListeners() {
        // 新建出差申请按钮
        newRequestButton.setOnClickListener(v -> {
            // 暂时注释掉对不存在类的引用
            // Intent intent = new Intent(BusinessTravelRequestListActivity.this, BusinessTravelRequestFormActivity.class);
            // startActivity(intent);
            Toast.makeText(this, "新建出差申请功能暂不可用", Toast.LENGTH_SHORT).show();
        });

        // 重试按钮
        retryButton.setOnClickListener(v -> loadBusinessTravelRequests());
    }

    /**
     * 加载出差申请数据
     */
    private void loadBusinessTravelRequests() {
        showLoadingState();
        try {
            // 获取当前用户的所有出差申请
            // 修改类型不匹配问题 - 暂时注释掉，因为getBusinessTripRequestsByApplicant返回的类型与我们需要的不匹配
            // List<BusinessTravelRequest> requests = viewModel.getBusinessTripRequestsByApplicant(currentUserId);
            List<BusinessTravelRequest> requests = new ArrayList<>(); // 使用空列表代替
            if (requests != null) {
                updateRecyclerView(requests);
            } else {
                showEmptyState();
            }
        } catch (Exception e) {
            showErrorState("加载出差申请失败: " + e.getMessage());
        } finally {
            progressBar.setVisibility(View.GONE);
        }
    }

    /**
     * 更新RecyclerView数据
     */
    private void updateRecyclerView(List<BusinessTravelRequest> requests) {
        if (requests.isEmpty()) {
            showEmptyState();
        } else {
            adapter.setBusinessTravelRequests(requests);
            recyclerView.setVisibility(View.VISIBLE);
            emptyStateTextView.setVisibility(View.GONE);
            errorTextView.setVisibility(View.GONE);
        }
    }

    /**
     * 显示加载状态
     */
    private void showLoadingState() {
        progressBar.setVisibility(View.VISIBLE);
        recyclerView.setVisibility(View.GONE);
        emptyStateTextView.setVisibility(View.GONE);
        errorTextView.setVisibility(View.GONE);
    }

    /**
     * 显示空状态
     */
    private void showEmptyState() {
        recyclerView.setVisibility(View.GONE);
        emptyStateTextView.setVisibility(View.VISIBLE);
        errorTextView.setVisibility(View.GONE);
    }

    /**
     * 显示错误状态
     */
    private void showErrorState(String errorMessage) {
        errorTextView.setText(errorMessage);
        recyclerView.setVisibility(View.GONE);
        emptyStateTextView.setVisibility(View.GONE);
        errorTextView.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 每次回到此页面时重新加载数据
        loadBusinessTravelRequests();
    }
}